package carrier;

import carrier.service.CarrierService;
import carrier.service.MessageService;
import com.example.utils.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

@SpringBootApplication
@EnableFeignClients
public class CarrierApplication {
    public static void main(String[] args) {
        SpringApplication.run(CarrierApplication.class, args);
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    private static final Logger LOG = LoggerFactory.getLogger(CarrierApplication.class);
    @Resource
    MessageService messageService;

    @KafkaListener(id = "orders", topics = "orders", groupId = "carrier")
    public void onEvent(Message m) {
        LOG.info("Received: {}" , m);
        if (m.getStatus().equals("DISPATCH"))
            messageService.busy(m);
        if (m.getStatus().equals("SIGNED"))
            messageService.free(m);
        else messageService.rollback(m);
    }
}
